﻿using DevExpress.Blazor;
using System.Linq.Expressions;
using Volo.Abp.DependencyInjection;
using YAKEJI.Xaf.DataDictionaries;
using YAPACS.BusinessObjects.Exams;

namespace YAPACS.RisServer.Interface.Imitate.ExamDetail
{
    /// <summary>
    /// 患者过滤器
    /// </summary>
    public class PatinetFilter:IPatinetFilter,ITransientDependency //注入瞬时依赖
    {
        private DataDictItem _IdCardType;
        private DataDictItem _MedicalInsuranceType;

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 曾用名
        /// </summary>
        public string OldName { get; set; }

        /// <summary>
        /// 亲属姓名
        /// </summary>
        public string ParentName { get; set; }

        /// <summary>
        /// 证件类型
        /// </summary>
        public DataDictItem IdCardType {
            get { 
                return _IdCardType;
            }
            set {
                _IdCardType = value;
            }
        }
        /// <summary>
        /// 证件号
        /// </summary>
        public string IdCardNumber { get; set; }

        /// <summary>
        /// 医保卡类型
        /// </summary>
        public DataDictItem MedicalInsuranceType 
        {
            get { return _MedicalInsuranceType; }
            set { 
                _MedicalInsuranceType = value;
            }
        }

        /// <summary>
        /// 医保卡号
        /// </summary>
        public string MedicalInsuranceNumber { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string PhoneNumber { get; set; }


        /// <summary>
        /// 联系方式2
        /// </summary>
        public string PhoneNumber2 { get; set; }

        /// <summary>
        /// 属性设置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="oldName"></param>
        /// <param name="parentName"></param>
        /// <param name="idCardType"></param>
        /// <param name="IdCardNumber"></param>
        public void SetProperties(string name ,string oldName,string parentName, DataDictItem idCardType,string idCardNumber,DataDictItem medicalInsuranceType,string medicalInsuranceNumber,string phoneNumber,string phoneNumber2)
        { 
            this.Name = name;
            this.OldName = oldName;
            this.ParentName = parentName;
            this.IdCardType = idCardType;
            this.IdCardNumber = idCardNumber;
            this.MedicalInsuranceType = medicalInsuranceType;
            this.MedicalInsuranceNumber = medicalInsuranceNumber;
            this.PhoneNumber = phoneNumber;
            this.PhoneNumber2 = phoneNumber2;
        }

        /// <summary>
        /// 构建表达式
        /// (动态构建)
        /// </summary>
        /// <returns></returns>
        public Expression<Func<Patient, bool>> ToExpression()
        {
            var predicate = PredicateBuilder.New<Patient>(true);

            if (!string.IsNullOrEmpty(Name))
            {
                predicate = predicate.And(p => p.Name.Contains(Name));
            }

            if (!string.IsNullOrEmpty(OldName))
            {
                predicate = predicate.And(e=>e.OldName.Contains(OldName));
            }

            if (!string.IsNullOrEmpty(ParentName))
            {
                predicate = predicate.And(p =>p.ParentName.Contains(ParentName));
            }

            if (IdCardType != null)
            {
                predicate = predicate.And(p=>p.IdCardType.Name.Contains(IdCardType.Name));
                
            }
            if (!string.IsNullOrEmpty(IdCardNumber))
            {
                predicate = predicate.And(p => p.IdCardNumber.Contains(IdCardNumber));
            }



            if (MedicalInsuranceType != null)
            {
                predicate = predicate.And(p => p.MedicalInsuranceType == MedicalInsuranceType);
            }

            if (!string.IsNullOrEmpty(MedicalInsuranceNumber))
            {
                predicate = predicate.And(p=>p.MedicalInsuranceNumber.Contains(MedicalInsuranceNumber));
            }

            if (!string.IsNullOrEmpty(PhoneNumber))
            {
                predicate = predicate.And(p=>p.PhoneNumber==PhoneNumber);
            }

            if (!string.IsNullOrEmpty(PhoneNumber2))
            {
                predicate = predicate.And(p=>PhoneNumber2==PhoneNumber2);
            }

            return predicate;
        }
    }
}
